// flatten.ts
export interface SaleInvoiceDetail {
  invoiceRecordDate: string
  invoiceLocalTotalAmount: string
  invoiceVoucherNum: string
}

export interface ReceiveBill {
  receiveRecordDate: string
  receiveAmount: string
  receiveVoucherNum: string
}

export interface PurchaseInvoiceDetail {
  invoiceRecordDate: string
  invoiceLocalTotalAmount: string
  invoiceVoucherNum: string
}

export interface PaymentBill {
  paymentRecordDate: string
  paymentAmount: string
  paymentVoucherNum: string
}

export interface PurchaseContractVO {
  purchaseContractId: string | null
  purchaseContractNum: string
  supplierName: string
  purchaseCurrency: number
  purchaseLocalTotalAmount: string | null
  purchaseLocalAmount: string
  purchaseLocalTaxedAmount: string
  purchaseInvoiceDetails: PurchaseInvoiceDetail[]
  paymentBills: PaymentBill[]
}

export interface SaleContractReportVO {
  saleContractId: string
  saleContractNum: string
  projectName: string
  transportSection: number
  saleCurrency: number
  contractLocalTotalAmount: string
  contractLocalAmount: string
  contractLocalTaxedAmount: string
  saleInvoiceDetails: SaleInvoiceDetail[]
  receiveBills: ReceiveBill[]
  purchaseContracts: PurchaseContractVO[]
}

// 扁平化单条销售合同
export function flattenSaleContract(vo: SaleContractReportVO) {
  const saleInvoiceLen = vo.saleInvoiceDetails.length || 0
  const receiveLen = vo.receiveBills.length || 0
  const purchaseLen = vo.purchaseContracts.length || 0

  const maxRows = Math.max(saleInvoiceLen, receiveLen, purchaseLen, 1)
  const rows: any[] = []

  for (let i = 0; i < maxRows; i++) {
    const mainRow: any = {
      // 主表字段
      saleContractId: vo.saleContractId,
      saleContractNum: vo.saleContractNum,
      projectName: vo.projectName,
      transportSection: vo.transportSection,
      saleCurrency: vo.saleCurrency,
      contractLocalTotalAmount: vo.contractLocalTotalAmount,
      contractLocalAmount: vo.contractLocalAmount,
      contractLocalTaxedAmount: vo.contractLocalTaxedAmount,

      // 销售发票
      invoiceRecordDate: vo.saleInvoiceDetails[i]?.invoiceRecordDate || '',
      invoiceLocalTotalAmount: vo.saleInvoiceDetails[i]?.invoiceLocalTotalAmount || '',
      invoiceVoucherNum: vo.saleInvoiceDetails[i]?.invoiceVoucherNum || '',

      // 收款
      receiveRecordDate: vo.receiveBills[i]?.receiveRecordDate || '',
      receiveAmount: vo.receiveBills[i]?.receiveAmount || '',
      receiveVoucherNum: vo.receiveBills[i]?.receiveVoucherNum || '',

      // 采购合同字段暂时为空
      purchaseContractId: '',
      purchaseContractNum: '',
      supplierName: '',
      purchaseCurrency: '',
      purchaseLocalTotalAmount: '',
      purchaseLocalAmount: '',
      purchaseLocalTaxedAmount: '',
      purchaseInvoiceRecordDate: '',
      purchaseInvoiceLocalTotalAmount: '',
      purchaseInvoiceVoucherNum: '',
      paymentRecordDate: '',
      paymentAmount: '',
      paymentVoucherNum: '',
    }
    rows.push(mainRow)

    // 处理采购合同
    if (vo.purchaseContracts[i]) {
      const pc = vo.purchaseContracts[i]
      const maxSubRows = Math.max(pc.purchaseInvoiceDetails.length, pc.paymentBills.length, 1)
      for (let j = 0; j < maxSubRows; j++) {
        rows.push({
          saleContractId: '',
          saleContractNum: '',
          projectName: '',
          transportSection: '',
          saleCurrency: '',
          contractLocalTotalAmount: '',
          contractLocalAmount: '',
          contractLocalTaxedAmount: '',

          // 采购合同
          purchaseContractId: pc.purchaseContractId,
          purchaseContractNum: pc.purchaseContractNum,
          supplierName: pc.supplierName,
          purchaseCurrency: pc.purchaseCurrency,
          purchaseLocalTotalAmount: pc.purchaseLocalTotalAmount,
          purchaseLocalAmount: pc.purchaseLocalAmount,
          purchaseLocalTaxedAmount: pc.purchaseLocalTaxedAmount,

          // 采购发票
          purchaseInvoiceRecordDate: pc.purchaseInvoiceDetails[j]?.invoiceRecordDate || '',
          purchaseInvoiceLocalTotalAmount: pc.purchaseInvoiceDetails[j]?.invoiceLocalTotalAmount || '',
          purchaseInvoiceVoucherNum: pc.purchaseInvoiceDetails[j]?.invoiceVoucherNum || '',

          // 付款
          paymentRecordDate: pc.paymentBills[j]?.paymentRecordDate || '',
          paymentAmount: pc.paymentBills[j]?.paymentAmount || '',
          paymentVoucherNum: pc.paymentBills[j]?.paymentVoucherNum || '',

          // 销售发票和收款为空
          invoiceRecordDate: '',
          invoiceLocalTotalAmount: '',
          invoiceVoucherNum: '',
          receiveRecordDate: '',
          receiveAmount: '',
          receiveVoucherNum: '',
        })
      }
    }
  }
  return rows
}

// 批量处理多条销售合同
export function flattenSaleContracts(vos: SaleContractReportVO[]) {
  const result: any[] = []
  vos.forEach(vo => {
    result.push(...flattenSaleContract(vo))
  })
  return result
}
